package br.com.fiap.beans;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;

import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.beanutils.BeanUtils;

import br.com.fiap.dao.VooDAO;
import br.com.fiap.entity.Voo;

@Named
@ManagedBean
@SessionScoped
public class VooBean extends AbstractBeanAcessor implements Serializable {
	
	private static final long serialVersionUID = 5351539652066588366L;
	
	private Voo voo = new Voo();
	
	@Inject
	private VooDAO vooDAO;

	public Voo getVoo() {
		return voo;
	}

	public void setVoo(Voo voo) {
		this.voo = voo;
	}
	
	public String novoVoo() {
		this.voo = new Voo();
		
		return "/voo/cadastro-voo";
	}
	
	public String  salvarDados() {
		
		vooDAO.saveOrUpdate(this.voo);
		
		this.voo = new Voo();
		
		return "/voo/sucesso-alteracao";
		
	}
	
	public String recuperarVoo(Integer numero) {
		Voo persistedVoo = vooDAO.findById(numero);
		
		try {
			BeanUtils.copyProperties(voo, persistedVoo);
		} catch (IllegalAccessException e) {
			System.out.println("Erro copyProperties.");
		} catch (InvocationTargetException e) {
			System.out.println("Erro copyProperties.");
		}
		
		return "/voo/cadastro-voo";
	}
	
}
